package weekMatch.one_343;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class LeeCode_02 {
    public static void main(String[] args) {
        System.out.println(firstCompleteIndex(new int[]{2, 8, 7, 4, 1, 3, 5, 6, 9}, new int[][]{
                {3, 2, 5},
                {1, 4, 6},
                {8, 7, 9}}));
    }
    private static int firstCompleteIndex(int[] arr, int[][] mat) {
        int m = mat.length;
        int n = mat[0].length;
        Map<Integer, Integer> hang = new HashMap<>();
        Map<Integer, Integer> lie = new HashMap<>();
        Map<Integer, int[]> map = new HashMap<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                map.put(mat[i][j], new int[]{i, j});
            }
        }
        for (int i = 0; i < arr.length; i++) {
            int num = arr[i];
            int h = map.get(num)[0];
            int l = map.get(num)[1];
            if (hang.containsKey(h)){
                hang.put(h, hang.get(h) + 1);
            }else {
                hang.put(h, 1);
            }
            if (lie.containsKey(l)){
                lie.put(l, lie.get(l) + 1);
            }else {
                lie.put(l, 1);
            }
            if (hang.get(h) == n || lie.get(l) == m){
                return i;
            }
        }
        return 0;
    }
}
